package gbench.sandbox.zk.zoo;

import org.apache.zookeeper.ZooKeeper.States;

/**
 * 
 * @author Administrator
 *
 */
public class ZKApp {

    /**
     * 
     * @param connectionString
     */
    public ZKApp(final String connectionString) {
        this.connectionString = connectionString;
    }

    /**
     * 
     * @param action 回调函数
     */
    public void withTransaction(final ExceptionalConsumer<ZKSession> action) {
        try {
            final var client = this.getSession();
            System.out.println(client.self().getState());
            client.start();
            System.out.println(States.CONNECTED);
            System.out.println(client.self().getState());
            action.accept(client);
            client.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 
     * @return
     */
    public ZKSession getSession() throws Exception {
        return new ZKSession(this.connectionString, this.connectionTimeout, this.sessionTimeout);
    }

    public String getConnectionString() {
        return connectionString;
    }

    public void setConnectionString(String connectionString) {
        this.connectionString = connectionString;
    }

    public int getSessionTimeout() {
        return sessionTimeout;
    }

    public void setSessionTimeout(int sessionTimeout) {
        this.sessionTimeout = sessionTimeout;
    }

    private String connectionString;
    private int sessionTimeout = 4000;
    private int connectionTimeout = 1000000;
}
